{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit InterfaceDescriptor;

interface

uses Classes, Expressions, Macros, Actions, Places, Animators, Layouts,
     BaseControls, Containers, Screens, MetaValues, Timers, Descriptor;

const
  FILE_INTERFACE_DESCRIPTOR = 'InterfaceDescriptor.xml';

type
  TAHMInterfaceDescriptor = class(TAHMDescriptor)
  private
    FExpressionList: TAHMExpressionList;
    FMacroList: TAHMMacroList;
    FActionList: TAHMActionList;
    FTimerList: TAHMTimerList;
    FMetaList: TAHMMetaValueList;
    FPlaceList: TAHMPlaceList;
    FAnimatorList: TAHMAnimatorList;
    FLayoutList: TAHMLayoutList;
    FControlList: TAHMControlList;
    FContainerList: TAHMContainerList;
    FScreenList: TAHMScreenLayoutList;
    procedure ParseFunctionList;
  protected
    procedure ParseXML; override;
    procedure SaveAsXML(Lines: TStrings); override;
    procedure SetBaseDescriptor(Base: TAHMDescriptor); override;
    function GetSaveRequired: Boolean; override;
  public
    constructor Create; override;
    destructor Destroy; override;
    property ExpressionList: TAHMExpressionList read FExpressionList;
    property MacroList: TAHMMacroList read FMacroList;
    property ActionList: TAHMActionList read FActionList;
    property TimerList: TAHMTimerList read FTimerList;
    property MetaList: TAHMMetaValueList read FMetaList;
    property PlaceList: TAHMPlaceList read FPlaceList;
    property AnimatorList: TAHMAnimatorList read FAnimatorList;
    property LayoutList: TAHMLayoutList read FLayoutList;
    property ControlList: TAHMControlList read FControlList;
    property ContainerList: TAHMContainerList read FContainerList;
    property ScreenList: TAHMScreenLayoutList read FScreenList;
    procedure Initialise; override;
    procedure Assign(Source: TAHMDescriptor); override;
    procedure Sort; override;
  end;


implementation

uses Screen, TagNames, XMLTags, ErrorCodes, SysUtils, UIObjects, XMLHelper;

constructor TAHMInterfaceDescriptor.Create;
begin
  inherited Create;

  FFileName := FILE_INTERFACE_DESCRIPTOR;
  FRootTagName := TAG_INTERFACE_DESCRIPTOR;

  FExpressionList := TAHMExpressionList.Create;
  FMacroList      := TAHMMacroList.Create;
  FActionList     := TAHMActionList.Create;
  FTimerList      := TAHMTimerList.Create;
  FMetaList       := TAHMMetaValueList.Create;
  FPlaceList      := TAHMPlaceList.Create;
  FAnimatorList   := TAHMAnimatorList.Create;
  FLayoutList     := TAHMLayoutList.Create;
  FControlList    := TAHMControlList.Create;
  FContainerList  := TAHMContainerList.Create;
  FScreenList     := TAHMScreenLayoutList.Create;
end;

destructor TAHMInterfaceDescriptor.Destroy;
begin
  FExpressionList.FreeWithContents;
  FMacroList.FreeWithContents;
  FActionList.FreeWithContents;
  FTimerList.FreeWithContents;
  FMetaList.FreeWithContents;
  FPlaceList.FreeWithContents;
  FAnimatorList.FreeWithContents;
  FLayoutList.FreeWithContents;
  FControlList.FreeWithContents;
  FContainerList.FreeWithContents;
  FScreenList.FreeWithContents;

  inherited Destroy;
end;

procedure TAHMInterfaceDescriptor.SetBaseDescriptor(Base: TAHMDescriptor);
begin
  // Set base list property for each of our list objects
  with Base as TAHMInterfaceDescriptor do
  begin
    Self.FExpressionList.BaseList := ExpressionList;
    Self.FMacroList.BaseList := MacroList;
    Self.FActionList.BaseList := ActionList;
    Self.FTimerList.BaseList := TimerList;
    Self.FMetaList.BaseList := MetaList;
    Self.FPlaceList.BaseList := PlaceList;
    Self.FAnimatorList.BaseList := AnimatorList;
    Self.FLayoutList.BaseList := LayoutList;
    Self.FControlList.BaseList := ControlList;
    Self.FContainerList.BaseList := ContainerList;
    Self.FScreenList.BaseList := ScreenList;
  end;
end;

function TAHMInterfaceDescriptor.GetSaveRequired: Boolean;
begin
  // Save required if any of the lists are populated
  Result := FExpressionList.RequiresSaving or FMacroList.RequiresSaving or
            FActionList.RequiresSaving or FTimerList.RequiresSaving or
            FMetaList.RequiresSaving or FPlaceList.RequiresSaving or
            FAnimatorList.RequiresSaving or FLayoutList.RequiresSaving or
            FControlList.RequiresSaving or FContainerList.RequiresSaving or
            FScreenList.RequiresSaving;
end;

procedure TAHMInterfaceDescriptor.ParseXML;
var
  RootNode: TAHMXMLNode;
begin
  // What nodes do we have here?
  RootNode := FHelper.CurrentNode;
  FParseNode := nil;

  while FHelper.NextElement(RootNode, FParseNode) do
  begin
    if FHelper.CurrentElement = TAG_FUNCTIONS then
      ParseFunctionList
    else if FHelper.CurrentElement = TAG_GLOBAL_EXPS then
      FExpressionList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_GLOBAL_MACROS then
      FMacroList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_GLOBAL_ACTIONS then
      FActionList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_TIMERS then
      FTimerList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_META_INFO then
      FMetaList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_PLACES then
      FPlaceList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_ANIMATORS then
      FAnimatorList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_LAYOUTS then
      FLayoutList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_CONTROLS then
      FControlList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_CONTAINERS then
      FContainerList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_SCREENS then
      FScreenList.ParseFromXML(FHelper)
    else
      LogParsingError(THM_PRS_INT_FOUND_UNKNOWN_CONTENT_TAG);
  end;
end;

procedure TAHMInterfaceDescriptor.SaveAsXML(Lines: TStrings);
begin
  // Write all interface descriptor lists to XML file
  FExpressionList.SaveAsXML(Lines);
  FMacroList.SaveAsXML(Lines);
  FActionList.SaveAsXML(Lines);
  FTimerList.SaveAsXML(Lines);
  FMetaList.SaveAsXML(Lines);
  FPlaceList.SaveAsXML(Lines);
  FAnimatorList.SaveAsXML(Lines);
  FLayoutList.SaveAsXML(Lines);
  FControlList.SaveAsXML(Lines);
  FContainerList.SaveAsXML(Lines);
  FScreenList.SaveAsXML(Lines);
end;

procedure TAHMInterfaceDescriptor.ParseFunctionList;
begin
  // Deprecated - skip parsing of function list for legacy themes
end;

procedure TAHMInterfaceDescriptor.Initialise;
begin
  inherited;

  // Initialise enabled timers
  FTimerList.InitialiseTimers;

  // Trigger any initialisation actions in theme
  FActionList.InvokeAction(AC_INIT);
end;

procedure TAHMInterfaceDescriptor.Assign(Source: TAHMDescriptor);
begin
  inherited;

  // Copy properties from interface descriptor
  with Source as TAHMInterfaceDescriptor do
  begin
    Self.ExpressionList.AssignContents(ExpressionList);
    Self.MacroList.AssignContents(MacroList);
    Self.ActionList.AssignContents(ActionList);
    Self.TimerList.AssignContents(TimerList);
    Self.MetaList.AssignContents(MetaList);
    Self.PlaceList.AssignContents(PlaceList);
    Self.AnimatorList.AssignContents(AnimatorList);
    Self.LayoutList.AssignContents(LayoutList);
    Self.ControlList.AssignContents(ControlList);
    Self.ContainerList.AssignContents(ContainerList);
    Self.ScreenList.AssignContents(ScreenList);
  end;
end;

procedure TAHMInterfaceDescriptor.Sort;
begin
  inherited;

{$IFDEF DESIGNER}
  FExpressionList.SortObjects;
  FMacroList.SortObjects;
  FActionList.SortObjects;
  FTimerList.SortObjects;
  FMetaList.SortObjects;
  FPlaceList.SortObjects;
  FAnimatorList.SortObjects;
  FLayoutList.SortObjects;
  FControlList.SortObjects;
  FContainerList.SortObjects;
  FScreenList.SortObjects;
{$ENDIF}
end;

end.

